const app = getApp();
Page({
	data: {
		url: app.globalData.host,
		pageId: 0,

		// tabList: ["走进基地","赛事培训","全民健身","产业创新","体育科普","赛事培训"],
		tabList: [],
		tabIndex: 0,

		list: [],
		briefImg: '',	// 简介图片
		briefId: 0,		// 简介id（顶部tab的对应id，查看简介需要）
		title: '',
		pageNum: 1,
		pageSize: 10,
		pageTotal: 0,
	},
	onLoad: function (options) {
		let title = options.name;
		wx.setNavigationBarTitle({
			title: title
		})
		this.setData({
			pageId: options.id
		});
	},
	onShow(){
		this.getTabList();
	},
	// 获取顶部列表 
	getTabList(){
		wx.showLoading({title: '加载中…'});
		app.request({
			url: '/app/projectCategory',
			method: 'GET',
			data: {
				parentId: this.data.pageId
			},
			success: res => {
				if(res.data.length !== 0){
					this.setData({
						tabList: res.data,
						briefImg: res.data[0].detailImg,
						briefId: res.data[0].id,
						title: res.data[0].categoryName,
						pageId: res.data[0].id
					})
				}
				this.getList();
			}
		})
	},
	getList(){
		let id = this.data.pageId;
		app.request({
			url: '/app/project',
			method: 'GET',
			data: {
				pageNum: this.data.pageNum,
				pageSize: this.data.pageSize,
				categoryId: id
			},
			success: res => {
				let list = [];
				if(this.data.pageNum == 1){
					list = res.rows;
				}else{
					list = this.data.list.concat(res.rows);
				}
				this.setData({
					list: list,
					pageTotal: res.total
				})
				wx.hideNavigationBarLoading() //完成停止加载
				wx.stopPullDownRefresh() //停止下拉刷新
			}
		})
		wx.hideLoading();
	},
	tabClick(e){
		let ind = e.currentTarget.dataset.index;
		let img = this.data.tabList[ind].detailImg;
		let id = e.currentTarget.dataset.id;
		if(e.currentTarget.dataset.index !== this.data.tabIndex){
			this.setData({
				tabIndex: ind,
				pageId: e.currentTarget.dataset.id,
				list: [],
				briefImg: img,
				briefId: id
			})
			wx.showLoading({title: '加载中…'});
			this.getList();
		}
	},
	toDetail(e){
		let id = e.currentTarget.dataset.id;
		wx.navigateTo({
			url: '../detail/detail?id=' + id
		})
	},
	toIntro(e) {
		let title = this.data.title + '简介';
		let id = this.data.briefId;
		wx.navigateTo({
			url: '../introduce/introduce?title=' + title + '&id=' + id
		})
	},
	// 上拉加载
	onReachBottom(){
		let total = this.data.pageTotal;
		let num = this.data.pageNum;
		if(this.data.list.length < total){
			this.setData({
				pageNum: num + 1
			})
			// wx.showLoading({title: '加载中…'});
			wx.showNavigationBarLoading(); //在标题栏中显示加载
			this.getList();
		}
	},
	//下拉刷新
	// onPullDownRefresh() {
	// 	this.setData({
	// 		list: []
	// 	})
	// 	wx.showNavigationBarLoading() //在标题栏中显示加载
	// 	let id = this.data.pageId;
	// 	app.request({
	// 		url: '/app/project',
	// 		method: 'GET',
	// 		data: {
	// 			pageNum: 1,
	// 			pageSize: this.data.pageSize,
	// 			categoryId: id
	// 		},
	// 		success: res => {
	// 			let list = [];
	// 			if(this.data.pageNum == 1){
	// 				list = res.data;
	// 			}else{
	// 				list = this.data.list.concat(res.data);
	// 			}
	// 			wx.hideNavigationBarLoading() //完成停止加载
	// 			wx.stopPullDownRefresh() //停止下拉刷新
	// 			this.setData({
	// 				list: list,
	// 				pageTotal: res.total,
	// 				pageNum: 1
	// 			})
	// 		}
	// 	})
	// }
})
